const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID
  const res = await db.collection('user').where({
    openid
  }).get()
  if (res.data.length) {
    return Promise.resolve(res.data[0])
  } else {
    return Promise.resolve(null)
  }
}